package com.gzhu.knowledgeAdmin.parammanage;

import com.gzhu.knowledgeAdmin.entity.Recorder;
import com.gzhu.knowledgeAdmin.repository.RecorderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Component
public class RecorderCache {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private RecorderRepository recorderRepository;

    private Map<String, Integer> name2AiIdMap = new HashMap<>();


    public Integer getAiIdByRecorderName(String name) {
        if (!name2AiIdMap.containsKey(name)) {
            Recorder recorder = recorderRepository.getRecorderByName(name);
            if (recorder != null) {
                name2AiIdMap.put(recorder.getName(), recorder.getAiId());
            }
        }

        return name2AiIdMap.get(name);
    }

    public void deleteCache() {
        name2AiIdMap = new HashMap<>();
    }

    public void deleteCacheByName(String name) {
        name2AiIdMap.remove(name);
    }

    public void putCache(String name, Integer aiId) {
        name2AiIdMap.put(name, aiId);
    }
}
